﻿/// <sumary>
/// Classe ConsultarMercadoCarteira - TBOCART
/// Data: 29/07/2008    
/// Autor: Marcelo Barucci
/// Obs: Criacao da Classe
/// </sumary>

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Sinacor.Servico.Bovespa.BovespaBase.Util;
using Sinacor.Servico.Bovespa.Ordens;
using Sinacor.Infra.Service.Data;
using Sinacor.Infra.Service.Security;
using Sinacor.Infra.Service.Security.Authorization;

namespace Sinacor.Servico.Bovespa.Ordens.ResourceAccess
{
    public class MercadoCarteira : BaseResourceAccess
    {
        public MercadoCarteira()
            : base(AuthorizationContext.Current.CompanyId) 
        {
        
        }
        public List<BusinessEntity.MercadoCarteira> ConsultarMercadoCarteira(BusinessEntity.Parameter.ConsultarMercadoCarteira pConsultarMercadoCarteria)
        {
            List<BusinessEntity.MercadoCarteira> response = new List<BusinessEntity.MercadoCarteira>();

            IDataReader reader;
            DbCommand dbCommand;            
            StringBuilder sql = new StringBuilder();

            sql.Append("select CD_CARLIQ, CD_ALMERC, CD_NATOPE, CD_BOLSA, NR_PRIORIDADE from TBOMERCAR ");
            sql.Append("where 0=0");

            if (pConsultarMercadoCarteria.CodigoCarteira != 0)
                sql.Append(" and CD_CARLIQ = :P_CD_CARLIQ");

            if (pConsultarMercadoCarteria.CodigoMercado != null)
                sql.Append(" and CD_ALMERC  = :P_CD_ALMERC");

            if (pConsultarMercadoCarteria.NaturezaOperacao != null)
                sql.Append(" and CD_NATOPE  = P_CD_NATOPE");

            if (pConsultarMercadoCarteria.CodigoBolsa != null)
                sql.Append(" and CD_BOLSA   = P_CD_BOLSA");

            dbCommand = this.DataBase.GetSqlStringCommand(sql.ToString());

            if (pConsultarMercadoCarteria.CodigoCarteira != 0)
                this.DataBase.AddInParameter(dbCommand, "P_CD_CARLIQ", DbType.AnsiString, pConsultarMercadoCarteria.CodigoCarteira);

            if (pConsultarMercadoCarteria.CodigoMercado != null)
                this.DataBase.AddInParameter(dbCommand, "P_CD_ALMERC", DbType.AnsiString, pConsultarMercadoCarteria.CodigoMercado);

            if (pConsultarMercadoCarteria.NaturezaOperacao != null)
                this.DataBase.AddInParameter(dbCommand, "P_CD_NATOPE", DbType.AnsiString, pConsultarMercadoCarteria.NaturezaOperacao);

            if (pConsultarMercadoCarteria.CodigoBolsa != null)
                this.DataBase.AddInParameter(dbCommand, "P_CD_BOLSA", DbType.AnsiString, pConsultarMercadoCarteria.CodigoBolsa);

            reader = this.DataBase.ExecuteReader(dbCommand);

            #region Mapeia o Objeto e adiciona na Lista
            while (reader.Read())
            {
                BusinessEntity.MercadoCarteira oMercadoCarteira = new BusinessEntity.MercadoCarteira();
                oMercadoCarteira.Carteira.CodigoCarteira = Convert.ToInt32(reader["CD_CARLIQ"].ToString());
                oMercadoCarteira.Mercado.Codigo = Convert.ToInt32(reader["CD_ALMERC"].ToString());
                oMercadoCarteira.NaturezaOperacao = (BusinessEntity.Enum.TipoNaturezaOperacao)(int)reader["CD_NATOPE"];
                oMercadoCarteira.Bolsa = reader["CD_BOLSA"].ToString();
                response.Add(oMercadoCarteira);
            }

            reader.Close();
            #endregion Mapeia o Objeto e adiciona na Lista            
            
            return response;

        }
    }
}
